<template>
<!--  <el-form :model="form" label-width="100px">-->
<!--    <el-form-item label="审核日期">-->
<!--      <el-col :span="230">-->
<!--        <el-date-picker-->
<!--          type="date"-->
<!--          placeholder="选择日期"-->
<!--          v-model="form.createDate"-->
<!--          style="width: 200px"-->
<!--          format="YYYY年MM月DD日"-->
<!--        ></el-date-picker>-->
<!--      </el-col>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="状态">-->
<!--      <el-input style="width: 350px" v-model="form.status"></el-input>-->
<!--    </el-form-item>-->
<!--  </el-form>-->
<!--  <el-form-item>-->
<!--    <el-button type="primary" @click="onSubmit">搜索</el-button>-->
<!--  </el-form-item>-->
  <img src="/src/assets/images/auditbg.jpg" style="height: 300px;width: 100%;border-radius: 20px">
  <!-- 数据表 -->
  <el-table
    ref="singleTable"
    :data="tableData"
    highlight-current-row
    style="width: 100%"
    :row-class-name="tableRowText"
  >
    <el-table-column property="modifier" label="修改人" width="150px">
    </el-table-column>
    <el-table-column property="auditor" label="审核人" width="150px">
    </el-table-column>
    <el-table-column property="createTime" label="创建时间" width="200px">
    </el-table-column>
    <el-table-column property="checkTime" label="审核时间" width="200px">
    </el-table-column>
    <el-table-column property="flightType" label="航班类型" width="150px">
    </el-table-column>

    <el-table-column label="状态">
      <template #default="scope">
        <span>{{ scope.row.checkStatus }}</span>
      </template>
    </el-table-column>

    <el-table-column label="操作" width="1200px">
      <template #default="scope">
        <el-button
          type="success"
          size="small"
          @click="handleEdit(scope.$index, scope.row)"
          
          >详情</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页组件 -->
  <el-pagination
    background
    layout="prev, pager, next"
    :total="count"
    :current-page="page"
    :pageCount="pageCount"
    @current-change="changePage"
  >
  </el-pagination>

  <el-dialog v-model="dialogVisible" width="70%">
    <h2 style="text-align: center">详情</h2>
    <el-table
      ref="dialogTable"
      :data="dialogData"
      highlight-current-row
      style="width: 100%"
      :row-class-name="tableRowClassName"
    >
      <el-table-column property="beforeAfter" label="更改前后">
      </el-table-column>
      <el-table-column property="dfltFlno" label="航班号"> </el-table-column>
      <el-table-column property="dfltAwcd" label="航空公司" width="150">
      </el-table-column>
      <el-table-column property="apcd" label="航线路由" width="150">
      </el-table-column>
      <el-table-column property="dtime" label="日期" width="200">
      </el-table-column>
      <el-table-column property="fptt" label="计划起飞时间" width="220">
      </el-table-column>
      <el-table-column property="fplt" label="预计降落时间" width="220">
      </el-table-column>
      <el-table-column property="flightStatus" label="原因" width="150">
      </el-table-column>
    </el-table>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button v-if="auditResult.status==1" type="primary" @click="accept"> 同意 </el-button>
        <el-button v-if="auditResult.status==1" type="danger" @click="refuser"> 驳回 </el-button>
      </span>
    </template>
  </el-dialog>


  <el-dialog v-model="dialogTransferVisible" width="70%">
    <h2 style="text-align: center">详情</h2>
    <el-table
      ref="dialogTable"
      :data="dialogTransferData"
      highlight-current-row
      style="width: 100%"
      :row-class-name="tableRowClassName"
    >
      <el-table-column property="beforeAfter" label="更改前后">
      </el-table-column>
      <el-table-column property="dfltFlno" label="航班号"> </el-table-column>
      <el-table-column property="dfltAwcd" label="航空公司" width="150">
      </el-table-column>
      <el-table-column property="dtime" label="日期" width="150">
      </el-table-column>
      <el-table-column property="apcd" label="第一站" width="150">
      </el-table-column>
     
      <el-table-column property="fptt" label="第一站时间" width="200">
      </el-table-column>

      <el-table-column property="transitApcd" label="第二站" width="150">
      </el-table-column>

      <el-table-column property="fplt" label="第二站时间" width="200">
      </el-table-column>
      <el-table-column property="flightStatus" label="原因" width="100">
      </el-table-column>
    </el-table>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button v-if="auditResult.status==1" type="primary" @click="accept"> 同意 </el-button>
        <el-button v-if="auditResult.status==1" type="danger" @click="refuser"> 驳回 </el-button>
      </span>
    </template>
  </el-dialog>


</template>

    <script>
import { ElMessage, ElMessageBox } from "element-plus";
export default {
  name: "check",
  data() {
    return {
      showButtons: true,
      auditResult: {},// 这玩意 是储存的所有数据？对，用来判断航班类型的
      checkStatus: "",
      dialogData: [{}],
      dialogVisible: false,
      dialogTransferData: [{}],
      dialogTransferVisible: false,
      
      selectLabel: 0,
      tableDataType: 1,
      called: false,
      formLabelWidth: "120px", //表单文本宽度
      tableData: [{}], //图书分页数据
      count: 0, //记录总数
      page: 1, //当前页码
      pageCount: "", //总页数
      form: {
        status: "",
        createDate: "2017-06-01T00:00:00.000Z",
      },
    };
  },
  created() {
    // let status = localStorage.getItem("showButtons");
    // if (status) {
    //   this.showButtons = status === "true";
    // }
  },
  methods: {
    tableRowText({ row, rowIndex }) {
      if (row.status === 2) {
        return "green-text";
      }
      if (row.status === 3) {
        return "red-text";
      }
      return "";
    },

    tableRowClassName({ row, rowIndex }) {
      if (rowIndex === 0) {
        return "warning-row";
      }
      if (rowIndex === 1) {
        return "success-row";
      }
      return "";
    },

    handleEdit(index, row) {
      console.log(row);
      if (row.flightType == "到港直达航班"||row.flightType == "离港直达航班") {
      this.dialogVisible = true;
      this.showdiaglog(row);
      }
      if (row.flightType == "到港中转航班"||row.flightType == "离港中转航班") {
      this.dialogTransferVisible = true;
      
      this.showdiaglog(row);
      }
      
      
    },
    cancel() {
      this.dialogVisible = false;
      this.dialogTransferVisible = false;
    },
    showdiaglog(row) {
      this.auditResult = row;
      const id = row.id;
      
      if (row.flightType == "离港直达航班") {
        this.$axios
          .get("http://localhost:8888/check/compareDateD2", {
            params: { id },
          })
          .then((response) => {
            if (response.data.status == 10000) {
              this.dialogData = response.data.data;
              this.dialogData[0].beforeAfter = "更改后";
              this.dialogData[1].beforeAfter = "更改前";
            } else {
              ElMessage({
                type: "error",
                message: "获取数据失败",
              });
            }
          });
      }
      if (row.flightType == "离港中转航班") {
        this.$axios
          .get("http://localhost:8888/check/compareDateD3", {
            params: { id },
          })
          .then((response) => {
            if (response.data.status == 10000) {
              this.dialogTransferData = response.data.data;
              this.dialogTransferData[0].beforeAfter = "更改后";
              this.dialogTransferData[1].beforeAfter = "更改前";
            } else {
              ElMessage({
                type: "error",
                message: "获取数据失败",
              });
            }
          });
      }

      if (row.flightType == "到港直达航班") {
        this.auditResult = row;
      const id = row.id;
        this.$axios
          .get("http://localhost:8888/check/compareDateA2", {
            params: { id },
          })
          .then((response) => {
            if (response.data.status == 10000) {
              this.dialogData = response.data.data;
              this.dialogData[0].beforeAfter = "更改后";
              this.dialogData[1].beforeAfter = "更改前";
            } else {
              ElMessage({
                type: "error",
                message: "获取数据失败",
              });
            }
          });
      }
      if (row.flightType == "到港中转航班") {
        this.auditResult = row;
      const id = row.id;
        this.$axios
          .get("http://localhost:8888/check/compareDateA3", {
            params: { id },
          })
          .then((response) => {
            if (response.data.status == 10000) {
              this.dialogTransferData = response.data.data;
              this.dialogTransferData[0].beforeAfter = "更改后";
              this.dialogTransferData[1].beforeAfter = "更改前";
            } else {
              ElMessage({
                type: "error",
                message: "获取数据失败",
              });
            }
          });
      }
    },
    changePage(currentPage) {
      this.page = currentPage;
      switch (this.tableDataType) {
        case 1:
          this.fetchData();
          break;
        case 2:
          this.onSubmit();
          break;
      }
    },
    fetchData() {
      const pageNum = this.page;
      const pageSize = 10; // 设置每页显示的数据条数
      this.$axios
        .get("http://localhost:8888/check/allcheck", {
          params: { pageNum, pageSize },
        })
        .then((response) => {
          const result = response.data.data;
          this.count = result.size;
          this.pageCount = Math.ceil(result.total / pageSize);
          this.tableData = result.list;
          this.tableDataType = 1;
          const list = result.list;
          list.forEach((item) => {
            const status = item.status;
            switch (status) {
              case 1:
                item.checkStatus = "未审核";
                break;
              case 2:
                item.checkStatus = "审核通过";
                break;
              case 3:
                item.checkStatus = "驳回";
                break;
              default:
                item.checkStatus = "未知状态";
                break;
            }
          });
        })
        .catch((error) => {
          console.error(error);
        });
    },

    accept() {
      const checkA2 = this.dialogData[0];
      const checkA3 = this.dialogTransferData[0];

      console.log(this.auditResult.status);

      if (this.auditResult.flightType == "离港直达航班") {
        this.$axios
          .post("http://localhost:8888/check/acceptD2", checkA2)
          .then((response) => {
            if (response.data.status == 10000) {
              ElMessage({
                type: "success",
                message: "审核成功",
              });
              this.dialogVisible = false;
              
              
              window.location.reload();
            } else {
              ElMessage({
                type: "error",
                message: "审核失败",
              });
            }
          });
      }
      if (this.auditResult.flightType == "离港中转航班") {
        this.$axios
          .post("http://localhost:8888/check/acceptD3", checkA3)
          .then((response) => {
            if (response.data.status == 10000) {
              ElMessage({
                type: "success",
                message: "审核成功",
              });
              this.dialogTransferData = false;
              
              window.location.reload();
            } else {
              ElMessage({
                type: "error",
                message: "审核失败",
              });
            }
          });
      }

      if (this.auditResult.flightType == "到港直达航班") {
        this.$axios
          .post("http://localhost:8888/check/acceptA2", checkA2)
          .then((response) => {
            if (response.data.status == 10000) {
              ElMessage({
                type: "success",
                message: "审核成功",
              });
              this.dialogVisible = false;
              
              window.location.reload();
            } else {
              ElMessage({
                type: "error",
                message: "审核失败",
              });
            }
          });
      }
      if (this.auditResult.flightType == "到港中转航班") {
       
        this.$axios
          .post("http://localhost:8888/check/acceptA3", checkA3)
          .then((response) => {
            if (response.data.status == 10000) {
              ElMessage({
                type: "success",
                message: "审核成功",
              });
              this.dialogTransferData = false;
              
              window.location.reload();
            } else {
              ElMessage({
                type: "error",
                message: "审核失败",
              });
            }
          });
      }
    },
    refuser() {
      const checkA2 = this.dialogData[0];
      const checkA3 = this.dialogTransferData[0];
      // this.showButtons = false; 
      // localStorage.setItem("showButtons", this.showButtons);
      if (this.auditResult.flightType == "离港直达航班") {
        this.$axios
          .post("http://localhost:8888/check/refuseD2", checkA2)
          .then((response) => {
            if (response.data.status == 10000) {
              ElMessage({
                type: "success",
                message: "审核成功",
              });
              this.dialogVisible = false;
              window.location.reload();
            } else {
              ElMessage({
                type: "error",
                message: "审核失败",
              });
            }
          });
      }
      if (this.auditResult.flightType == "离港中转航班") {
        this.$axios
          .post("http://localhost:8888/check/refuseD3", checkA3)
          .then((response) => {
            if (response.data.status == 10000) {
              ElMessage({
                type: "success",
                message: "审核成功",
              });
              this.dialogTransferData = false;
              window.location.reload();
            } else {
              ElMessage({
                type: "error",
                message: "审核失败",
              });
            }
          });
      }

      if (this.auditResult.flightType == "到港直达航班") {
        this.$axios
          .post("http://localhost:8888/check/refuseA2", checkA2)
          .then((response) => {
            if (response.data.status == 10000) {
              ElMessage({
                type: "success",
                message: "审核成功",
              });
              this.dialogVisible = false;
              window.location.reload();
            } else {
              ElMessage({
                type: "error",
                message: "审核失败",
              });
            }
          });
      }
      if (this.auditResult.flightType == "到港中转航班") {
        this.$axios
          .post("http://localhost:8888/check/refuseA3", checkA3)
          .then((response) => {
            if (response.data.status == 10000) {
              ElMessage({
                type: "success",
                message: "审核成功",
              });
              this.dialogTransferData = false;
              window.location.reload();
            } else {
              ElMessage({
                type: "error",
                message: "审核失败",
              });
            }
          });
      }
    },

    confirm() {
      ElMessageBox.confirm("你确认要修改吗", "Warning", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
    },
  },
  mounted() {
    this.fetchData();
  },
};
</script>
    

<style>
.el-table .warning-row {
  background: rgba(255, 0, 0, 0.5);
}
/* .el-table .success-row {
  background: #928ce5;
} */

.red-text {
  color: red;
}

.orange-text {
  color: orange;
}

.green-text {
  color: green;
}

</style>
